/**
 *
 * \file CPixmap.cpp
 * \brief This file contains the CPixmap class implementation.
 * \author Vladimir Klubkov (aka marked)
 *
 * Last changes date:
 * \date 15.03.2011
 *
 * Copyright 2011 Vladimir Klubkov <marked-one@yandex.ru>
 *
 * This file is part of OOXlib.
 *
 * OOXLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * OOXlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OOXlib; if not, you can try to find  it here:
 * http://www.gnu.org/licenses/gpl.txt
 * or write to the Free Software Foundation, Inc., 59 Temple Place,
 * Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "../include/CPixmap.h"

namespace OOXlib {

/**
 * Constructor. Calls the base-class constructor.
 * \param File Source code file name.
 * \param iLine Source code line.
 * \param Function Function name.
 */
CPixmap::CNULLPtrFilePathException::CNULLPtrFilePathException(const ::std::string &File, const int iLine, const ::std::string &Function)
: CException(File, iLine, Function, "Can't create the CPixmap object. Empty string was specified as the pixmap file path.") {}
/**
 * Constructor. Calls the base-class constructor.
 * \param File Source code file name.
 * \param iLine Source code line.
 * \param Function Function name.
 */
CPixmap::CXpmReadFileToPixmapException::CXpmReadFileToPixmapException(const ::std::string &File, const int iLine, const ::std::string &Function)
: CException(File, iLine, Function, "Can't create the CPixmap object. Maybe the wrong pixmap file path was specified. The XpmReadFileToPixmap function failed.") {}
/**
 * Constructor. Creates the pixmap from file.
 * \param pXServerConnection A pointer to the CXServerConnection object.
 * \param pScreen A pointer to the CScreen object.
 * \param pszPixmapPath A null-terminated string contatining the pixmap path.
 */
CPixmap::CPixmap(const CXServerConnection *pXServerConnection, const CScreen *pScreen, const ::std::string &PixmapPath)
: m_pXServerConnection(pXServerConnection) {
    if(NULL == m_pXServerConnection) {
        OOXLib_THROW(CNULLPtrConnectionException);
    }
    if(NULL == pScreen) {
        OOXLib_THROW(CNULLPtrScreenException);
    }
    if(PixmapPath.empty()) {
        OOXLib_THROW(CNULLPtrFilePathException);
    }
    ::XpmAttributes xpma;
    xpma.valuemask = 0;
    // As seen in some source code, available somewhere on the web, the ::XpmReadFileToPixmap function returns nil on success,
    // but, unfortunately, I haven't found this in any help. And yes, we need 'const_cast', cause the file name isn't const here...
    // Note: this function is from the Xpm library.
    if(0 != ::XpmReadFileToPixmap(pXServerConnection->Get(), static_cast< ::Drawable>(::XRootWindow(pXServerConnection->Get(), pScreen->Get())), const_cast<char*>(PixmapPath.c_str()), &m_Pixmap, &m_Mask, &xpma)) {
        OOXLib_THROW(CXpmReadFileToPixmapException);
    }
    m_PixmapPath = PixmapPath;
    m_Size.Set(xpma.width, xpma.height);
    ::XpmFreeAttributes(&xpma);
}
/**
 * Destructor. Calls the XFreePixmap function to free the memory, allocated by the XpmReadFileToPixmap.
 */
CPixmap::~CPixmap() {
    // XFreePixmap can generate a BadPixmap error, but it's called from dtor...
    ::XFreePixmap(m_pXServerConnection->Get(), m_Pixmap);
}
/**
 * Returns the Pixmap.
 * \return The Pixmap.
 */
::Pixmap CPixmap::Get() const {
    return m_Pixmap;
}
/**
 * Returns the pixmap mask.
 * \return The pixmap mask.
 */
::Pixmap CPixmap::GetMask() const {
    return m_Mask;
}
/**
 * Returns the Pixmap.
 * \return The Pixmap.
 */
CPixmap::operator ::Pixmap() const {
    return m_Pixmap;
}
/**
 * Returns the pixmap path.
 * \return The pixmap path string.
 */
::std::string CPixmap::GetPath() const {
    return m_PixmapPath;
}
CUInt2dSize CPixmap::GetSize() const {
    return m_Size;
}
unsigned int CPixmap::GetWidth() const {
    return m_Size.GetX();
}
unsigned int CPixmap::GetHeight() const {
    return m_Size.GetY();
}

} // OOXlib
